package hibernate.web;

import hibernate.fileControler.MyException;
import hibernate.model.Aygit;
import hibernate.model.Bellekler;
import hibernate.model.Buzdolabi;
import hibernate.model.Klima;
import hibernate.model.Mp3ipod;
import hibernate.model.Resimler;
import hibernate.model.Sirketprofil;
import hibernate.model.Telefon;
import hibernate.model.Tv;
import hibernate.service.EmmyService;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

@SuppressWarnings("serial")
@Component("AygitBean")
@Scope("session")
public class AygitBean implements BeanService {

	@Autowired
	private transient EmmyService emmyService;
	
	private Aygit selectedAygit;
	private Object selectedAygitObject; //seçilen Bellek, TV vb...
	private ArrayList<String> selectedAygitObjectResList;	
	private List<Aygit> aygitList;
	private ArrayList <Object> aygitlar =new ArrayList<Object>(); //Bellek, Tv vb çeşitli aygıtları tutmak için...	
	
	private transient SirketprofilBean sirketProBean;
	private boolean edit;
	private boolean yeniUrun;
	
	@SuppressWarnings("unchecked")
	@Override
	@PostConstruct
	public void init() {
		aygitList = (List<Aygit>) emmyService.findAll(new Aygit());
		
		aygitlar.clear(); // önceden doluysa iptal etmek için
		if(aygitList != null){
			for (Iterator<?> iter = aygitList.iterator(); iter.hasNext();) {
		        	Aygit aygt = (Aygit) iter.next();
		      /*  	for(Iterator<?> iter2 = aygt.getResimlers().iterator(); iter2.hasNext();) {		        		
			        	Resimler res = (Resimler)iter2.next();
		        		System.out.println("yolu : "+res.getResimKonum());
		        		System.out.println("aygit id : "+aygt.getAygitid());
		        	}*/
		        	getListe(aygt); // aygitlar listesini doldurma 
			}		
		}
	}

	@Override
	public String restart() {
		deleteSelectedAygit();
		deleteSelectedSirket();
		init();
		return "aygitList";
	}

	@Override
	public String deleteView() {
		emmyService.delete(selectedAygit);
		return "list";
	}

	@Override
	public String viewNew() {
		selectedAygit= new Aygit();
		return "detail";
	}

	@Override
	public String viewDetail() { /*
		List<Aygit> valueList = (List<Aygit>) aygitTable.getValue();	
		System.out.println("table size : "+valueList.size());		
		Iterator<SimpleRowKey> keys = aygitTable.getSelection().getKeys();
		
		if(keys.hasNext()){
			currentAygit= valueList.get(keys.next().intValue());
			System.out.println("dolu = "+currentAygit.getAygitid());
			return "detail";
		}else{
			System.out.println("bo�ta");
			return null;
		}*/ 
		System.out.println("malibu");
		return "detay";
	}	

	@Override
	public String SaveOrUpdate() throws MyException {
		emmyService.saveOrUpdate(selectedAygit);
		init();
		return "list";
	}
	
	public void editAygit(){
		FacesMessage msg = null; 
		edit = true;
		try{
		if(selectedAygitObject.getClass().getSimpleName().equals("Bellekler")){
			BelleklerBean blkBean = (BelleklerBean) emmyService.getBean(new BelleklerBean());
			blkBean.setSelectedBellek((Bellekler)selectedAygitObject);
			( (ResimlerBean) emmyService.getBean(new ResimlerBean()) ).getSelectedAygitResimleri(
					((Bellekler)selectedAygitObject).getAygitid());
		}else if(selectedAygitObject.getClass().getSimpleName().equals("Buzdolabi")){
			BuzdolabiBean bzdBean = (BuzdolabiBean) emmyService.getBean(new BuzdolabiBean());
			bzdBean .setSelectedBuzdolabi((Buzdolabi) selectedAygitObject);
			( (ResimlerBean) emmyService.getBean(new ResimlerBean()) ).getSelectedAygitResimleri(
					((Buzdolabi)selectedAygitObject).getAygitid());
		}else if(selectedAygitObject.getClass().getSimpleName().equals("Klima")){
			KlimaBean klmBean = (KlimaBean) emmyService.getBean(new KlimaBean());
			klmBean.setSelectedKlima((Klima) selectedAygitObject);
			( (ResimlerBean) emmyService.getBean(new ResimlerBean()) ).getSelectedAygitResimleri(
					((Klima)selectedAygitObject).getAygitid());
		}else if(selectedAygitObject.getClass().getSimpleName().equals("Mp3ipod")){
			Mp3ipodBean mpiBean = (Mp3ipodBean) emmyService.getBean(new Mp3ipodBean());
			mpiBean.setSelectedMp3ipod((Mp3ipod) selectedAygitObject);
			( (ResimlerBean) emmyService.getBean(new ResimlerBean()) ).getSelectedAygitResimleri(
					((Mp3ipod)selectedAygitObject).getAygitid());
		}else if(selectedAygitObject.getClass().getSimpleName().equals("Telefon")){
			TelefonBean tlfBean = (TelefonBean) emmyService.getBean(new TelefonBean());
			tlfBean.setSelectedTelefon((Telefon) selectedAygitObject);
			( (ResimlerBean) emmyService.getBean(new ResimlerBean()) ).getSelectedAygitResimleri(
					((Telefon)selectedAygitObject).getAygitid());
		}else if(selectedAygitObject.getClass().getSimpleName().equals("Tv")){
			TvBean tvBean = (TvBean) emmyService.getBean(new TvBean());
			tvBean.setSelectedTv((Tv) selectedAygitObject);
			( (ResimlerBean) emmyService.getBean(new ResimlerBean()) ).getSelectedAygitResimleri(
					((Tv)selectedAygitObject).getAygitid());
		}else{
			msg = new FacesMessage(FacesMessage.SEVERITY_WARN, "Hata", "Seçim yapılmadı");
			FacesContext.getCurrentInstance().addMessage(null, msg);
			edit = false;			
		}
		}catch(NullPointerException npe){
			msg = new FacesMessage(FacesMessage.SEVERITY_WARN, "Hata", "Seçim yapılmadı");
			FacesContext.getCurrentInstance().addMessage(null, msg);
		}
	}
	@SuppressWarnings({ "unchecked" })
	public String getUreticiAygitlari(Sirketprofil sp) {	
		System.out.println("Üretici Aygitlari");
		System.out.println("sp : "+sp.getFirmaAd());
		System.out.println("sp : "+sp.getFirmaid());
		
		aygitList = (List<Aygit>) emmyService.getQuery("findAygitUretici", "auretici", sp.getFirmaid());
		
		aygitlar.clear(); // önceden doluysa iptal etmek için
		if(aygitList != null){
			for (Iterator<?> iter = aygitList.iterator(); iter.hasNext();) {
		        	Aygit aygt = (Aygit) iter.next();    
		        	getListe(aygt); // aygitlar listesini doldurma   	
			}		
		}
		
		selectedAygitObject =null;
		return "aygitList"; // Bu dönüş değeri ileride değişebilir
	}
	
	@SuppressWarnings("unchecked")
	public String getUreticiAygitTur(long id, String AygitTuru) {
		List<Aygit> aygtLst = (List<Aygit>) emmyService.
			 	getQuery("from Aygit where ureticiid ="+id+" and aygitTuru ="+AygitTuru+"");	
		
		aygitlar = null; // önceden doluysa iptal etmek için
		
		for (Iterator<?> iter = aygtLst.iterator(); iter.hasNext();) {		        	
		        	Aygit aygt = ((Aygit) iter.next());
		        	getListe(aygt); // aygitlar listesini doldurma		        	
		}	
		
		return ""; // Bu dönüş değeri ileride değişebilir
	} 
	
	public void getListe(Aygit aygt){
		
		if(aygt.getAygitTuru().equals("BELLEK")){
			aygitlar.add(aygt.getBellekler());
    	}
    	else if(aygt.getAygitTuru().equals("BUZDOLABI")){
    		aygitlar.add(aygt.getBuzdolabi());
    	}
    	else if(aygt.getAygitTuru().equals("KLIMA")){
    		aygitlar.add(aygt.getKlima());
    	}
    	else if(aygt.getAygitTuru().equals("MP3IPOD")){
    		aygitlar.add(aygt.getMp3ipod());
    	}
    	else if(aygt.getAygitTuru().equals("TELEFON")){
    		aygitlar.add(aygt.getTelefon());
    	}
    	else if(aygt.getAygitTuru().equals("TV")){ 
    		aygitlar.add(aygt.getTv());
    		for(Iterator<?> iter2 = aygt.getResimlers().iterator(); iter2.hasNext();) {		        		
	        	Resimler res = (Resimler)iter2.next();
        		System.out.println("yolu : "+res.getResimKonum());
        		System.out.println("aygit id : "+aygt.getAygitid());
        	}
    	}
	}
	
	public void deleteSelectedSirket(){ //deleteFirmaToContext
		System.out.println("111111111111");
		sirketProBean = (SirketprofilBean)emmyService.getBean(new SirketprofilBean());
		sirketProBean.deleteSelectedSirket();
		//httpSession.removeAttribute("currentFirma");
	}
	
	public void deleteSelectedAygit(){ //deleteAygitToContext		
		selectedAygitObjectResList =null;
		selectedAygitObject =null;
		yeniUrun = false;
		
	}
	public void getSelectedAygitResList(){
		System.out.println("aygit resimleri");
		selectedAygitObjectResList = emmyService.getCurrentAygitResList(selectedAygitObject);
	}
	
	public String urunIslemleri(){ // ürün işlemleri sayfası event'ı için...
		try{
			System.out.println("Ürün işlemleri giriş");
			sirketProBean = (SirketprofilBean) emmyService.getBean(new SirketprofilBean());
			getUreticiAygitlari(sirketProBean.getCurrentSirketpro());
			sirketProBean.setSelectedSirket(sirketProBean.getCurrentSirketpro());
			System.out.println("Ürün işlemleri çıkış");
			return "sirketAygitList";
		}catch(Exception e){
			System.out.println("Ürün işlemleri hatası: "+e);
			return "index";
		}
	}
		
	@Override
	public void getSession(){
		
	}
	
	@Override
	public String createQuery() {
		// TODO Auto-generated method stub
		return null;
	}
	
	public List<Aygit> getAygitList() {
		return aygitList;
	}

	public void setAygitList(List<Aygit> aygitList) {
		this.aygitList = aygitList;
	}

	public EmmyService getEmmyService() {
		return emmyService;
	}

	public void setEmmyService(EmmyService emmyService) {
		this.emmyService = emmyService;
	}

	public ArrayList<Object> getAygitlar() {
		return aygitlar;
	}

	public void setAygitlar(ArrayList<Object> aygitlar) {
		this.aygitlar = aygitlar;
	}

	public Aygit getSelectedAygit() {
		return selectedAygit;
	}

	public void setSelectedAygit(Aygit selectedAygit) {
		this.selectedAygit = selectedAygit;
	}

	public Object getSelectedAygitObject() {
		return selectedAygitObject;
	}

	public void setSelectedAygitObject(Object selectedAygitObject) {
		this.selectedAygitObject = selectedAygitObject;
	}

	public ArrayList<String> getSelectedAygitObjectResList() {
		return selectedAygitObjectResList;
	}

	public void setSelectedAygitObjectResList(
			ArrayList<String> selectedAygitObjectResList) {
		this.selectedAygitObjectResList = selectedAygitObjectResList;
	}

	public SirketprofilBean getSirketProBean() {
		return sirketProBean;
	}

	public void setSirketProBean(SirketprofilBean sirketProBean) {
		this.sirketProBean = sirketProBean;
	}

	public boolean isEdit() {
		return edit;
	}

	public void setEdit(boolean edit) {
		this.edit = edit;
	}

	public boolean isYeniUrun() {
		return yeniUrun;
	}

	public void setYeniUrun(boolean yeniUrun) {
		this.yeniUrun = yeniUrun;
	}
}